# coding:utf-8
# __author__ = yuan
# __time__ = 2020/4/22
# __file__ = explore
# __desc__ =
import glob
import os
from pathlib import Path

import cv2
import numpy as np
import random
from PIL import Image
convd_dir=r"F:\Resources\kdata\covid-chestxray-dataset\images"
norm_dir = r"F:\Resources\kdata\chest_xray\train\NORMAL"
def adsize(dir):
    sizes=[]
    for img in glob.iglob(f"{dir}\\*.*g"):
        im:Image.Image = Image.open(img)
        h,w =im.size
        sizes.append((h,w))
    hs,ws = list(zip(*sizes))
    print(f"高度:\n"
          f"最大值：{max(hs)} "
          f"最小值：{min(hs)} "
          f"平均值：{sum(hs)/len(hs)}\n")
    print(f"宽度:\n"
          f"最大值：{max(ws)} "
          f"最小值：{min(ws)} "
          f"平均值：{sum(ws) / len(ws)}\n")

# adsize(convd_dir)
# adsize(norm_dir)
def num_combine(n,m):
    def factorial(s):
        if s<=1:
            return 1
        return s*(factorial(s-1))
    return factorial(n) // (factorial(m)*(factorial(n-m)))
def show(im,prefix=''):
    random.seed(random.randint(0,9))
    g = lambda: ''.join([random.choice('sfdughnbvczsjzvmblkjghsfuytwrifm')for _ in range(4)])
    cv2.imshow(prefix+"_"+g(), im)
    cv2.waitKey(6000)
    cv2.destroyAllWindows()
def morgr():
    # print(num_combine(4,2))
    p=r"F:\Resources\kdata\chest_xray\train\NORMAL\IM-0115-0001.jpeg"
    # p2=r"F:\Resources\kdata\covid-chestxray-dataset\images\1.CXRCTThoraximagesofCOVID-19fromSingapore.pdf-001-fig2b.png"
    # img = cv2.imread(p2)
    nimg = cv2.imread(p)
    # img=cv2.resize(img,(299,900))
    nimg=cv2.resize(nimg,(299,299))
    # show(img,'covid')
    show(nimg,'normal')
    # diff = cv2.absdiff(nimg,img)
    # show(diff)
    # im:Image.Image = Image.open(p2)
    # im=im.resize((700,700),resample=2)
    # im.show('s')
morgr()

def get_nb_files(directory):
    """Get number of files by searching directory recursively"""
    if not Path(directory).exists():
        return 0
    cnt = 0
    for r, dirs, files in os.walk(directory):
        if files:
            cnt+=len(files)
    return cnt

dird=r"F:\Resources\kdata\chest_xray\train"
# print(get_nb_files(dird))